package com.wangyk.user.controller;

import com.wangyk.rabbitmqcommon.VO.ResultVO;
import com.wangyk.rabbitmqcommon.enums.ResultEnum;
import com.wangyk.rabbitmqcommon.enums.RoleEnum;
import com.wangyk.rabbitmqcommon.model.user.UserInfo;
import com.wangyk.rabbitmqcommon.utils.CookieUtil;
import com.wangyk.rabbitmqcommon.utils.ResultVOUtils;
import com.wangyk.user.constant.CookieConstant;
import com.wangyk.user.constant.RedisConstant;
import com.wangyk.user.repository.UserRepository;
import com.wangyk.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/login")
@Slf4j
public class LoginController
{
    @Autowired
    private UserService userService;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 买家登录，将openid存入cookie
     */
    @RequestMapping("/buyer")
    @ResponseBody
    public ResultVO buyer(@RequestParam("openId") String openId, HttpServletResponse response)
    {
        ResultVO resultVo = new ResultVO();
        //1.openId和数据库的数据匹配
        UserInfo userInfo = userRepository.findByOpenid(openId);
        if (userInfo == null)
        {
            return ResultVOUtils.error(ResultEnum.LOGIN_FAILED);
        }
        //2.查看角色
        Integer role = Integer.valueOf(userInfo.getRole());
        if (role != RoleEnum.BUYER.getCode())
        {
            return ResultVOUtils.error(ResultEnum.ROLE_ERROR);
        }
        log.info(String.valueOf(role));
        //3.设置cookies
        CookieUtil.set(response, CookieConstant.OPENID, userInfo.getOpenid(), CookieConstant.EXPIRE);
        resultVo.setData(userInfo);
        return ResultVOUtils.success(resultVo);
    }

    /**
     * 卖家登录，将token存入cookie，将token存入redis
     */
    @RequestMapping("/seller")
    @ResponseBody
    public ResultVO seller(@RequestParam("openId") String openid, HttpServletResponse response,
        HttpServletRequest request)
    {
        //判断是否已经登录过
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        if (cookie != null && !StringUtils.isEmpty(stringRedisTemplate.opsForValue()
            .get(String.format(RedisConstant.TOKEN_TEMPLATE, cookie.getValue()))))
        {
            return ResultVOUtils.success();
        }
        ResultVO resultVo = new ResultVO();
        //1.openId和数据库的数据匹配
        UserInfo userInfo = userRepository.findByOpenid(openid);
        if (userInfo == null)
        {
            return ResultVOUtils.error(ResultEnum.LOGIN_FAILED);
        }
        //2.查看角色
        Integer role = Integer.valueOf(userInfo.getRole());
        if (role != RoleEnum.SELLER.getCode())
        {
            return ResultVOUtils.error(ResultEnum.ROLE_ERROR);
        }
        //3.设置Redis
        Integer expire = CookieConstant.EXPIRE;
        String token = UUID.randomUUID().toString();
        stringRedisTemplate.opsForValue().set(String.format(RedisConstant.TOKEN_TEMPLATE, token), openid, expire,
            TimeUnit.SECONDS);
        //4.设置cookies
        CookieUtil.set(response, CookieConstant.TOKEN, token, CookieConstant.EXPIRE);
        resultVo.setData(userInfo);
        return ResultVOUtils.success(resultVo);
    }
}
